package pro.shushi.pamirs.hr.core.action;

import org.springframework.stereotype.Component;
import pro.shushi.pamirs.hr.api.enums.ApprovalStatusEnum;
import pro.shushi.pamirs.hr.api.enums.HrSimpleExpEnum;
import pro.shushi.pamirs.hr.api.extpoint.BusinessTripServiceExtpoint;
import pro.shushi.pamirs.hr.api.model.biz.BusinessTrip;
import pro.shushi.pamirs.hr.core.session.HrSimpleSession;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.api.Ext;
import pro.shushi.pamirs.meta.api.session.PamirsSession;
import pro.shushi.pamirs.meta.common.exception.PamirsException;
import pro.shushi.pamirs.meta.constant.ExpConstants;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.ActionContextTypeEnum;
import pro.shushi.pamirs.meta.enmu.FunctionOpenEnum;
import pro.shushi.pamirs.meta.enmu.FunctionTypeEnum;
import pro.shushi.pamirs.meta.enmu.ViewTypeEnum;

import java.util.Date;

@Slf4j
@Component
@Model.model(BusinessTrip.MODEL_MODEL)
public class BusinessTripAction {

    /**
     * 自定义构造方法示例
     * 如：界面设计器提交数据-选择的提交函数，新增时系统会调用construct
     * @param data
     * @return
     */
    @Function(openLevel = FunctionOpenEnum.API)
    @Function.Advanced(type = FunctionTypeEnum.QUERY)
    public BusinessTrip construct(BusinessTrip data) {
        data.setEmployName(HrSimpleSession.getEmployeeName());
        data.setStartTime(new Date());
        return data;
    }

    @Action.Advanced(name = FunctionConstants.create, managed = true, invisible = ExpConstants.idValueExist)
    @Action(displayName = "保存", summary = "添加", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.create)
    @Function.fun(FunctionConstants.create)
    public BusinessTrip create(BusinessTrip data) {
        // 预留扩展点，进行一些个性化的处理
        Ext.run(BusinessTripServiceExtpoint::customHandler, data);

        data.construct();
        data.create();
        return data;
    }

    @Action(displayName = "提交", summary = "提交后不能进行编辑", bindingType = ViewTypeEnum.TABLE, contextType = ActionContextTypeEnum.SINGLE)
    // 可以在界面设计器设置如果是非NC状态按钮隐藏掉或者无效
    @Action.Advanced(invisible = "activeRecord.approvalStatus != 'NC'")
    public BusinessTrip submit(BusinessTrip data) {
        BusinessTrip dbBusinessTrip = new BusinessTrip().setId(data.getId()).queryById();
        if (!ApprovalStatusEnum.NC.equals(dbBusinessTrip.getApprovalStatus())) {
            throw PamirsException.construct(HrSimpleExpEnum.TRIP_STATUS_HAS_PENDING).errThrow();
        }

        BusinessTrip update = new BusinessTrip();
        update.setId(data.getId());
        update.setApprovalStatus(ApprovalStatusEnum.PENDING);
        update.updateById();
        return data;
    }

    @Action(displayName = "审核通过", bindingType = ViewTypeEnum.TABLE, contextType = ActionContextTypeEnum.SINGLE)
    // 可以在界面设计器设置如果是非NC状态按钮隐藏掉或者无效
    @Action.Advanced(invisible = "activeRecord.approvalStatus != 'PENDING'")
    public BusinessTrip approve(BusinessTrip data) {
        BusinessTrip dbBusinessTrip = new BusinessTrip().setId(data.getId()).queryById();
        if (!ApprovalStatusEnum.PENDING.equals(dbBusinessTrip.getApprovalStatus())) {
            throw PamirsException.construct(HrSimpleExpEnum.TRIP_STATUS_HAS_PENDING).errThrow();
        }

        // 实际业务是，更新的时候可以new一个对象出来在去更新，减少更新的字段
        // 根据条件更新另参考：https://doc.oinone.top/backend/11467.html
        BusinessTrip update = new BusinessTrip();
        update.setId(data.getId());
        update.setApprovalStatus(ApprovalStatusEnum.APPROVED);
        update.setAuditTime(new Date());
        update.setAuditUser(PamirsSession.getUserName());
        update.updateById();
        return data;
    }

    @Action(displayName = "审核拒绝", bindingType = ViewTypeEnum.TABLE, contextType = ActionContextTypeEnum.SINGLE)
    // 可以在界面设计器设置如果是非NC状态按钮隐藏掉或者无效
    @Action.Advanced(invisible = "activeRecord.approvalStatus != 'PENDING'")
    public BusinessTrip reject(BusinessTrip data) {
        BusinessTrip dbBusinessTrip = new BusinessTrip().setId(data.getId()).queryById();
        if (!ApprovalStatusEnum.PENDING.equals(dbBusinessTrip.getApprovalStatus())) {
            throw PamirsException.construct(HrSimpleExpEnum.TRIP_STATUS_HAS_PENDING).errThrow();
        }

        BusinessTrip update = new BusinessTrip();
        update.setId(data.getId());
        update.setApprovalStatus(ApprovalStatusEnum.APPROVED);
        update.setAuditTime(new Date());
        update.setAuditUser(PamirsSession.getUserName());
        update.updateById();
        return data;
    }

}
